package com.kehongborui.spring.controller;

import com.kehongborui.spring.dao.UserMapper;
import com.kehongborui.spring.exception.SpringException;
import com.kehongborui.spring.model.UserModel;
import com.kehongborui.spring.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-01
 */
@Controller // @Component @Service @Respo
@RequestMapping("/login")
public class LoginController extends BaseController {
    public static final String LOGIN_USER = "LOGIN_USER";
    @Autowired
    private LoginService loginService;
    /**
     * 获取登录页面
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String loginPage() {
        return "login";
    }

    /**
     * 提交登录数据
     * @param req
     * @return
     */
//    @RequestMapping(method = RequestMethod.POST)
    public String toLogin(HttpServletRequest req) {
        // 从req 中获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserModel model = new UserModel();
        model.setUsername(username);
        model.setPassword(password);
        // 调用service 进行登录验证
        model = loginService.login(model);
        // 将数据放在session中，共页面使用
        req.getSession().setAttribute("LOGIN_USER", model);
        // 将数据放到req中共页面
        req.setAttribute("user", model);
        return "success";
    }

//    @RequestMapping(method = RequestMethod.POST)
    public String toLogin(String username, @RequestParam("password") String password, HttpServletRequest req) {
        // 从req 中获取参数

        UserModel model = new UserModel();
        model.setUsername(username);
        model.setPassword(password);
        // 调用service 进行登录验证
        model = loginService.login(model);
        req.setAttribute("user", model);
        return "success";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String toLogin(UserModel model, HttpServletRequest request, ModelMap map) {
        // 调用service 进行登录验证
        try {
            model = loginService.login(model);
        } catch (RuntimeException e) {
            map.addAttribute("error", e.getMessage());
            return "login";
        }

        map.addAttribute("user", model);  // == req.setAttribute("user", model);
        // 将登录信息放到session,供 interceptor做权限认证使用
        request.getSession().setAttribute(LOGIN_USER, model);
        System.out.println("enter Controller toLogin ================>");
//        throw new RuntimeException("吧啦吧啦！");
        return "success";
        // 重定向方式1
//        return "redirect:https://www.baidu.com";
    }

//    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView toLoginRedirect(UserModel model, ModelMap map) {
        // 调用service 进行登录验证
        try {
            model = loginService.login(model);
        } catch (RuntimeException e) {
            map.addAttribute("error", e.getMessage());
            // 登录失败，返回到登录页面
            return new ModelAndView("login");
        }

        // 重定向方式1
        RedirectView redirectView = new RedirectView("https://www.baidu.com");
        ModelAndView view = new ModelAndView(redirectView);

        return view;
    }
    @ModelAttribute("current_controller_val")
    public String global() {
        return "helloword!!!!!!!!!!!!!!!!!!!!";
    }
}
